package com.lemon.smartsafety.ui.activity.baidu;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.baidu.BaiduMapEvent;
import com.lemon.smartsafety.mode.adapter.BaiduAddressMode;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.adapter.baidu.BaiduAddressAdapter;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.PoiSearchCityUtil;
import com.lemon.smartsafety.utils.SystemUtil;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.lemon.smartsafety.view.ViewMapSearch;
import com.lemon.smartsafety.view.list.ListRefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class LemonBaiduSearchActivity extends NewBaseActivity {


    @Bind(R.id.viewMapSearch)
    ViewMapSearch viewMapSearch;
    @Bind(R.id.listView)
    ListRefreshView listView;
    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;


    private BaiduAddressAdapter baiduAddressAdapter;
    private List<BaiduAddressMode> mList;

    private PoiSearchCityUtil poiSearchCityUtil;
    private BaiduAddressMode searchMode = null;
    private String strCity = "";

    //搜索的关键字
    private String strSearchEdt="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            strCity = getIntent().getExtras().getString(BundleKey.MAP_SEARCH);
        }
    }

    @Override
    protected int getContentViewId() {
        SDKInitializer.initialize(getApplicationContext());
        return R.layout.activity_lemon_baidu_search;
    }

    @Override
    protected void initTitleView() {

    }

    @Override
    protected void initView() {
        toolbar.setVisibility(View.GONE);

        if (CommonUtil.strIsEmpty(strCity)) {
            callFinish();
        }


        viewMapSearch.setSendClick(new ViewMapSearch.AllClick() {
            @Override
            public void click() {
                SystemUtil.HideKeyboard(mActivity);
                if (CommonUtil.strIsEmpty(viewMapSearch.getEdtText())) {
                    ToastUtil.show("请输入搜索内容");
                } else {
                    current_page = 1;
                    searchPoi(strCity, viewMapSearch.getEdtText());
                    strSearchEdt=viewMapSearch.getEdtText();
                }
            }
        });
        viewMapSearch.setBackClick(new ViewMapSearch.AllClick() {
            @Override
            public void click() {
                callFinish();
            }
        });

        poiSearchCityUtil = new PoiSearchCityUtil(mActivity);

        mList = new ArrayList<>();
        baiduAddressAdapter = new BaiduAddressAdapter(mActivity, mList);
        listView.setAdapter(baiduAddressAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                SystemUtil.HideKeyboard(mActivity);
                AppService.getBus().post(new BaiduMapEvent(mList.get(position)));
                mActivity.finish();
            }
        });
        listView.setScrollBottomListener(new ListRefreshView.ScrollBottomListener() {
            @Override
            public void scrollBottom() {
                bPutHttp = true;

                try {
                    showUpLoad(true, listView);
                    current_page = current_page + 1;
                    searchPoi(strCity, strSearchEdt);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        poiSearchCityUtil.onDestroy();
    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }


    /**
     * 搜索
     */
    private void searchPoi(String city, String keyWord) {

        poiSearchCityUtil.setLoadIndex(current_page - 1);
        poiSearchCityUtil.searchCity(city, keyWord, new PoiSearchCityUtil.CallBackInterface() {
            @Override
            public void callBackFunction(boolean isSuccess) {

                showUpLoad(false, listView);
                if (!isSuccess) {
                    bPutHttp = false;
                    ToastUtil.show("未找到更多位置信息");
                    return;
                }

                if (CommonUtil.listIsEmpty(poiSearchCityUtil.getPoiResult().getAllPoi())) {
                    return;
                }
                if (current_page == 1) {
                    LogUtils.LOGE("第一次加载清空,清空列表");
                    mList.clear();
                    baiduAddressAdapter.notifyDataSetChanged();
                }

                for (int i = 0; i < poiSearchCityUtil.getPoiResult().getAllPoi().size(); i++) {
                    BaiduAddressMode baiduAddressMode = new BaiduAddressMode(poiSearchCityUtil.getPoiResult().getAllPoi().get(i).name, poiSearchCityUtil.getPoiResult().getAllPoi().get(i).address, poiSearchCityUtil.getPoiResult().getAllPoi().get(i).location);
                    baiduAddressMode.setUid(poiSearchCityUtil.getPoiResult().getAllPoi().get(i).uid);
                    baiduAddressMode.setAddress(poiSearchCityUtil.getPoiResult().getAllPoi().get(i).address);
                    baiduAddressMode.setCity(poiSearchCityUtil.getPoiResult().getAllPoi().get(i).city);

                    mList.add(baiduAddressMode);
                }
                baiduAddressAdapter.notifyDataSetChanged();

                if (mList.size() % 10 == 0) {
                    listView.setIsload(true);
                } else {
                    listView.setIsload(false);
                }

                if (current_page == 1) {
                    listView.setSelection(0);
                }

            }
        });
    }
}